import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
/*import Mock from './mock'
Mock.bootstrap();*/
import 'font-awesome/css/font-awesome.min.css'

import axios from 'axios'
//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})
/*路由拦截器*/
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login') {/*判断路劲是不是login，如果是则删除token*/
    sessionStorage.removeItem('token');
    sessionStorage.removeItem('menu');
    sessionStorage.removeItem('info');
    sessionStorage.removeItem('permission');
    initIndexRouters();
  }
  if (to.path == '/ShopRegister') {/*判断路径是不是ShopRegister，如果是则放行*/
    next()
    return  /*如果进入到里面，则终止后边的运行*/
  }
  let user = sessionStorage.getItem('token');
  if (!user && to.path != '/login') {/*如果token为空且路径不是login，则重新进入到login页面*/
    next({ path: '/login' })
  } else {
    next()
  }
})
// 静态拦截器   to 到哪里 from 从哪里来  next 放行 去哪里
//1 使用axios前置拦截器，让所有的请求都携带Token
axios.interceptors.request.use(config=>{
  //携带token
  let token =  sessionStorage.getItem("token");
  if(token){
    config.headers['token']=token;
  }
  return config;
},error => {
  Promise.reject(error);
})

//2使用axios后置拦截器，处理没有登录请求
axios.interceptors.response.use(result=>{
  let data = result.data;//响应的结果
  if(!data.success && data.message==="Nologin"){
    location.href = "/login.html";
  }
  if(data.success && data.message==="refresh"){
    sessionStorage.setItem("token",data.token);
  }
  if(!data.success && data.message==="NoPermission"){
    alert("没有权限")
  }
  return result;//如果不返回  有下文  但是你的那些接口 就拿不到返回值了
},error => {
  Promise.reject(error);
})

//router.afterEach(transition => {
//NProgress.done();
//});
initIndexRouters();
new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')


function initIndexRouters(){
  if(!sessionStorage.menu){
    return;
  }
  //防止重复配置路由
  if(router.options.routes.length>5){
    return;
  }
  let menus = sessionStorage.getItem('menu');
  menus = JSON.parse(menus);
  let tempRouters = [];
  menus.forEach(menu=>{
    let indexRouter = {
      path: '/',
      iconCls: menu.icon,
      name: menu.name,
      component: resolve => require(['@/views/Home'], resolve),
      children: []
    }
    menu.children.forEach(cMenu=>{
      let cr = {
        path: cMenu.url,
        name: cMenu.name,
        component: resolve => require(['@/views/'+cMenu.component], resolve)
      }
      indexRouter.children.push(cr)
    })
    tempRouters.push(indexRouter)
    router.options.routes.push(indexRouter)
  })
  //动态路由配置
  console.log(tempRouters)
  router.addRoutes(tempRouters);
}